<template>
  <div class="app-container">
    <!-- 搜索 -->
    <div style="display: flex; margin-bottom: 20px">
      <div>
        <el-input
          v-model="params.search"
          size="small"
          :clearable="true"
          placeholder="请输入文件名"
        />
      </div>
      <div style="margin-left: 20px">
        <el-button
          type="primary"
          size="small"
          icon="el-icon-search"
          @click="searchList"
        >搜索</el-button>
      </div>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="ID" width="80" prop="id" />

      <el-table-column align="center" label="图片名称" width="250" prop="name" />
      <el-table-column align="center" label="图片保存路径" width="150" prop="url">
        <template slot-scope="scope">
          <img :src="scope.row.url" width="50px" height="50px">
        </template>
      </el-table-column>
      <el-table-column
        label="图片上传时间"
        width="180"
        align="center"
        prop="picUploadTime"
      >
        <template slot-scope="scope">
          {{ new Date(scope.row.created_at).toLocaleString() }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="filetype" label="文件类型" width="150">
        <template slot-scope="scope">
          <span v-if="scope.row.filetype == 1">图片</span>
          <span v-if="scope.row.filetype == 2">文档</span>
          <span v-if="scope.row.filetype == 3">视频</span>
          <span v-if="scope.row.filetype == 4">音频</span>
          <span v-if="scope.row.filetype == 5">文件</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="200">
        <template slot-scope="scope">
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="delInfo(scope.row.id)"
          >删除</el-button>
        </template>
      </el-table-column>

    </el-table>

    <!--分页-->
    <el-pagination
      background
      :current-page="params.page"
      :page-sizes="[15, 30, 45, 60]"
      :page-size="params.limit"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
import { getResource } from '@/api/resource'
import { del } from '@/api/resource'// 上传图片引用

export default {
  components: {
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      woc: '',
      showAdd: false,
      list: null,
      listLoading: true,
      fileList: [],
      rules: {},
      dialogFormVisible: false,
      total: 10,
      params: {
        page: 1,
        limit: 15,
        search: ''
      },
      form: {
        id: '',
        name: '',
        url: '',
        created_at: '',
        filetype: ''
      },
      newType: []
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    searchList() {
      this.fetchData()
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.params.limit = val
      this.fetchData(false)
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.params.page = val
      this.fetchData(false)
    },
    //  加载页面数据
    async fetchData(load = true) {
      this.listLoading = true
      await getResource(this.params).then((response) => {
        this.list = response.data.data
        this.total = response.data.total
        setTimeout(() => {
          this.listLoading = false
        }, 500)
      }).catch(() => {
        this.listLoading = false
      })
    },
    // 删除
    delInfo(id) {
      // 执行后端 删除方法
      this.$confirm('你确定要删除该资源, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del(id).then((res) => {
          this.fetchData(false)
          this.$message({
            type: 'success',
            message: '删除成功！'
          })
        })
      })
    }

  }
}
</script>
